package com.lanou.musicbox.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.SongsAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.Songs;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 赵宝龙 on 15/10/21.
 * 歌单页面
 */
public class SongsFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private GridView playlistGridView;
    private SongsAdapter adapter;
    private VolleySingleton volleySingleton;
    private List<Songs.ContentEntity> datas;
    private FragmentManager manager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_songs, null);
        playlistGridView = (GridView) v.findViewById(R.id.playlistGridView);
        playlistGridView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        initData();// 定义一个方法用来解析

        return v;
    }

    private void initData() {
        datas = new ArrayList<>();
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.diy.gedan&page_size=30&page_no=1";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Songs s = gson.fromJson(response, Songs.class);
               // Log.e("ZBL", "===================" + response);
                datas = s.getContent();
               // Log.e("ZBL", "===================" + datas.get(0).getCollectnum());
                adapter = new SongsAdapter(getActivity(), datas);
                playlistGridView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       // mainFragmentActivity = (MainFragmentActivity) getActivity();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //mainFragmentActivity.jump(adapter.getUrl(i));
        manager = getActivity().getSupportFragmentManager();
       EnterTheSongsFragment fragment = new EnterTheSongsFragment();

        Bundle bundle = new Bundle();
        bundle.putString("songs_id", adapter.getUrl(i));
        fragment.setArguments(bundle);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.fill_main, fragment);
        transaction.commit();
    }
}

